/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import edu.syr.iis.web.model.AnnotationSession;
import edu.syr.iis.web.model.AnnotationSessionManager;
import edu.syr.iis.web.model.AnnotationSessionStatistics;
import edu.syr.iis.web.model.Conversation;
import edu.syr.iis.web.model.Dataset;
import edu.syr.iis.web.model.User;
import edu.syr.iis.web.model.UserManager;
import edu.syr.iis.web.util.Key;

/**
 * @author kinoue
 *
 */
public class AnnotationSessionListController  extends AbstractController 
{
	AnnotationSessionManager annotationSessionManager;
	UserManager userManager;

	// Model Objects
	List<Conversation> conversations = null;

	private String userView;
	private String adminView;
	
	static final Date projectStartedAt = Date.valueOf("2011-03-02");

	@Override
	public ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse response) 
	throws Exception
	{	
		ModelAndView modelAndView = null; 
				
		User user = (User) request.getSession().getAttribute(Key.USER);
		List<AnnotationSession> annotationSessions = null;
		List<AnnotationSessionStatistics> stats = new ArrayList<AnnotationSessionStatistics>();
		
		
		if (user.getRole() == User.ANNOTATOR)
		{
			modelAndView = new ModelAndView(userView);
			
			Date today = new Date(new java.util.Date().getTime());
			Date aWeekAgo = new Date(today.getTime() - Key.weekInMillis);
			Date date = (Date) projectStartedAt.clone(); 
			
			annotationSessions = annotationSessionManager.getAnnotationSessions(user, aWeekAgo, today);

			while (date.before(today))
			{
				Date endDate = new Date(date.getTime() + Key.weekInMillis);				
				AnnotationSessionStatistics stat = annotationSessionManager.getStat(user, date, endDate);
				stats.add(stat);
				date = new Date(date.getTime() + Key.weekInMillis);
			}
			
			modelAndView.addObject(Key.SESSION_STATS, stats);
			modelAndView.addObject(Key.SESSIONS, annotationSessions);			
			
		}
		else
        {
			modelAndView = new ModelAndView(userView);
			List<User> users = userManager.getUsers();
			
			for (User u: users)
			{
				Date today = new Date(new java.util.Date().getTime());
				Date aWeekAgo = new Date(today.getTime() - Key.weekInMillis);
				Date date = (Date) projectStartedAt.clone(); 
				
				annotationSessions = annotationSessionManager.getAnnotationSessions(aWeekAgo, today);
			
				while (date.before(today))
				{
					Date endDate = new Date(date.getTime() + Key.weekInMillis);				
					AnnotationSessionStatistics stat = annotationSessionManager.getStat(u, date, endDate);
					stats.add(stat);
					date = new Date(date.getTime() + Key.weekInMillis);
				}
			
				modelAndView.addObject(Key.SESSION_STATS, stats);
				modelAndView.addObject(Key.SESSIONS, annotationSessions);
			}
		}
		
		return modelAndView;
	}

	
	public void setAnnotationSessionManager(
			AnnotationSessionManager annotationSessionManager) {
		this.annotationSessionManager = annotationSessionManager;
	}


	public void setUserView(String userView) {
		this.userView = userView;
	}

	public void setAdminView(String adminView) {
		this.adminView = adminView;
	}


	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

}
